package com.zlx.mall.biz.goods.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zlx.mall.base.entity.ScCombo;
import com.zlx.mall.base.entity.ScComboCategory;
import com.zlx.mall.base.entity.ScGoods;
import com.zlx.mall.base.service.IScComboCategoryService;
import com.zlx.mall.base.service.IScComboService;
import com.zlx.mall.base.service.IScGoodsService;
import com.zlx.mall.basic.common.enums.NotDeteteEnum;
import com.zlx.mall.basic.common.exception.IllegalBizArgumentException;
import com.zlx.mall.basic.web.model.BaseRequest;
import com.zlx.mall.biz.goods.model.GoodsDetailsResponse;
import com.zlx.mall.biz.goods.model.GoodsListResponse;
import com.zlx.mall.biz.goods.service.IGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author xiatian
 * @Date 2025/3/23 星期日 18:58
 * @注释
 */
@Slf4j
@Service
public class GoodsServiceImpl implements IGoodsService {
    @Autowired
    private IScComboCategoryService scComboCategoryService;
    @Autowired
    private IScGoodsService scGoodsService;
    @Autowired
    private IScComboService scComboService;

    @Override
    public List<GoodsListResponse> list(BaseRequest request) {
        String selectCode = request.asText("/selectCode");
        ScCombo combo = Optional.ofNullable(scComboService.getOne(new QueryWrapper<ScCombo>().eq(ScCombo.SELECT_CODE, selectCode).eq(ScCombo.IS_DELETE, NotDeteteEnum.NOT_DELETE.getCode())))
                .orElseThrow(() -> new IllegalBizArgumentException("套餐不存在"));
        Long subCategoryId = request.asValidLong("/subCategoryId", "二级类目ID不能为空");
        List<ScComboCategory> list = scComboCategoryService.list(new QueryWrapper<ScComboCategory>().eq(ScComboCategory.COMBO_ID, combo.getId()).eq(ScComboCategory.SUB_CATEGORY_ID, subCategoryId)
                .eq(ScComboCategory.IS_DELETE, NotDeteteEnum.NOT_DELETE.getCode()));
        if (CollUtil.isNotEmpty(list)) {
            List<Long> goodsIds = list.stream().map(ScComboCategory::getGoodsId).collect(Collectors.toList());
            List<ScGoods> goodsList = scGoodsService.list(new QueryWrapper<ScGoods>().in(ScGoods.ID, goodsIds).eq(ScGoods.IS_DELETE, NotDeteteEnum.NOT_DELETE.getCode()));
            List<GoodsListResponse> responses = new ArrayList<>();
            for (ScGoods goods : goodsList) {
                GoodsListResponse response = new GoodsListResponse();
                response.setId(goods.getId());
                response.setName(goods.getName());
                response.setMainImg(goods.getMainImg());
                responses.add(response);
            }
            return responses;
        }
        return Collections.emptyList();
    }

    @Override
    public GoodsDetailsResponse details(Long goodsId) {
        ScGoods scGoods = Optional.ofNullable(scGoodsService.getById(goodsId)).orElseThrow(() -> new IllegalBizArgumentException("商品不存在"));

        GoodsDetailsResponse response = new GoodsDetailsResponse();
        response.setId(scGoods.getId());
        response.setName(scGoods.getName());
        response.setSalePrice(scGoods.getSalePrice());
        response.setLabel(scGoods.getLabel());
        response.setImages(scGoods.getMainImg());
        response.setDetailsImg(scGoods.getDetailsImg());
        response.setModel(scGoods.getModel());
        response.setMaterial(scGoods.getMaterial());
        response.setProperty(scGoods.getProperty());
        return response;
    }
}
